package com.ahaoyou.core;

import com.ahaoyou.utils.FastJsonUtils;
import com.ahaoyou.utils.StringUtil;
import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.Properties;

import static com.ahaoyou.utils.ConfigUtil.*;

public class JcProperties extends Properties {

    public JcProperties() {
    }

    public String getString(String key) {
        Object val = get(key);
        if (val == null) {
            return null;
        }
        return String.valueOf(val);
    }

    public Long getLong(String key) {
        String val = getString(key);
        if (val == null) {
            return null;
        }
        return Long.parseLong(val);
    }

    public List<String> getStrList(String key) {
        if (StringUtil.isEmpty(key)) {
            return null;
        }
        String val = getString(key);
        if (StringUtil.isNotEmpty(val)) {
            return FastJsonUtils.toList(val, String.class);
        }
        return null;
    }

    public <T> T getBean(String key, Class<T> clz) {
        T bean = FastJsonUtils.toBean(JSON.toJSONString(get(key)), clz);
        if (bean != null) {
            return bean;
        }
        try {
            T instance = clz.newInstance();
            invoke(instance, this, "");
            return instance;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}